﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Homework3
{
    public abstract class Shape
    {
        public abstract double GetArea();
        public abstract bool IsLegal();
    }

    public class Rectangle : Shape
    {
        public double Width              //设置宽属性
        { get; set; }

        public double Height            //设置高属性
        { get; set; }

        public Rectangle(double width, double height)
        {
            this.Width = width;
            this.Height = height;
        }
        //判断是否合法
        public override bool IsLegal()
        {
            return (this.Width > 0 && this.Height > 0);
        }
        //算面积
        public override double GetArea()
        {
            if (this.IsLegal())
                return this.Width * this.Height;
            else
                return -1;
        }
    }

    public class Square : Shape
    {
        public double Width         //边长属性
        { get; set; }
        public Square(double width)
        {
            this.Width = width;
        }
        //判断是否合法
        public override bool IsLegal()
        {
            return (this.Width > 0);
        }
        //算面积
        public override double GetArea()
        {
            if (this.IsLegal())
                return this.Width * this.Width;
            else
                return -1;
        }
    }

    public class Triangle : Shape
    {
        public double A
        { get; set; }
        public double B
        { get; set; }
        public double C
        { get; set; }

        public Triangle(double a, double b, double c)
        {
            this.A = a;
            this.B = b;
            this.C = c;
        }
        //判断合法性
        public override bool IsLegal()
        {
            if (this.A > 0 && this.B > 0 && this.C > 0)
                return (this.A + this.B > this.C && this.A + this.C > this.B && this.B + this.C > this.A);
            else return false;
        }
        //算面积
        public override double GetArea()
        {
            if (this.IsLegal())
            {
                double p = (this.A + this.B + this.C) / 2;
                return Math.Sqrt(p * (p - this.A) * (p - this.B) * (p - this.C));
            }
            else
                return -1;
        }
    }

    public class ShapeFactory
    {
        public enum ShapeType { Square, Rectangle, Triangle }
        public Shape Create()
        {
            Random r = new Random(Guid.NewGuid().GetHashCode());
            int type = r.Next(3);
            Shape shape = null;
            ShapeType shapeType = (ShapeType)type;
            switch (shapeType)
            {
                case ShapeType.Square:
                    Console.WriteLine("创建正方形");
                    shape = new Square(r.NextDouble() * 10);
                    break;
                case ShapeType.Rectangle:
                    Console.WriteLine("创建长方形");
                    shape = new Rectangle(r.NextDouble() * 10, r.NextDouble() * 10);
                    break;
                case ShapeType.Triangle:
                    Console.WriteLine("创建三角形");
                    shape = new Triangle(r.NextDouble() * 10, r.NextDouble() * 10, r.NextDouble() * 10);
                    break;
            }
            return shape;
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            double sum = 0;
            int shapeSum = 0;
            ShapeFactory factory = new ShapeFactory();
            while (shapeSum < 10)
            {

                double area = factory.Create().GetArea();
                if (area != -1)
                {
                    Console.WriteLine($"第{shapeSum + 1}个图形的面积是 " + area);
                    shapeSum++;
                    sum += area;
                }
                else
                    Console.WriteLine("该图形不合法 ");
            }
            Console.WriteLine("总面积为 " + sum);
        }
    }
}

